//  This file is part of Swordbible - http://www.swordwarrior.net
//  Copyright (C) 2003-2009 Manu Mital mital.manu@gmail.com

//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2, or (at your option)
//  any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.



// VersePicker.cpp : implementation file
//

#include <stdafx.h>
#include "SwordBible.h"
#include "VersePicker.h"
#include "SwordBibleDoc.h"

// CVersePicker dialog

IMPLEMENT_DYNAMIC(CVersePicker, CDialog)

CVersePicker::CVersePicker(CWnd* pParent /*=NULL*/)
	: CDialog(CVersePicker::IDD, pParent)
{
	key = "";

}

CVersePicker::~CVersePicker()
{
}

void CVersePicker::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_VP_BOOK, m_cmbBook);
	DDX_Control(pDX, IDC_COMBO_VP_CHAPTER, m_cmbChapter);
	DDX_Control(pDX, IDC_COMBO_VP_VERSE, m_cmbVerse);
}


BEGIN_MESSAGE_MAP(CVersePicker, CDialog)
   ON_CBN_SELCHANGE(IDC_COMBO_VP_BOOK, OnSelectBook)
   ON_CBN_SELCHANGE(IDC_COMBO_VP_CHAPTER, OnSelectChapter)
   ON_BN_CLICKED(IDOK, &CVersePicker::OnBnClickedOk)
END_MESSAGE_MAP()


// CVersePicker message handlers

BOOL CVersePicker::OnInitDialog()
{
	CDialog::OnInitDialog();

	

	/* Fill in the names of the Bible book in the Bible book combo */
	for( int i = 0; i < 66; i++)
	m_cmbBook.AddString(CSwordBibleDoc::bookCode[i][0]);

	// set the selection to Genesis
	m_cmbBook.SetCurSel(0);


	//find the number of chapters in the currently selected book and fill the combo box
	 oldChapCount =  _tstoi( CSwordBibleDoc::bookCode[m_cmbBook.GetCurSel()][2] );
	 CString number;
	 for(int i = 0; i < oldChapCount; i++) {
		 number.Format(_T("%d"),i+1);			 
		 m_cmbChapter.AddString(number);
	 }

	// set the chapter in the bottom module combo box
	 m_cmbChapter.SetCurSel(0);	
	 m_cmbChapter.LimitText(3);


 // get the total number of verses in the newly selected chapter
	oldVerseCount = CSwordBibleDoc::VerseCount[m_cmbBook.GetCurSel()][ m_cmbChapter.GetCurSel()];
	 
	
	 for(int i = 0; i < oldVerseCount; i++) {
		 number.Format(_T("%d"),i+1);			 
		 m_cmbVerse.AddString(number);
	 }

	// set the verse in the bottom module's verse combo box
     m_cmbVerse.SetCurSel(0);	
	 m_cmbVerse.LimitText(3);
	

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


// this function is called when the user selects a book of the bible from
// the combo box
void CVersePicker::OnSelectBook()
{
	// the 0 based index of the new book that the user has selected
	int selectedBook  = m_cmbBook.GetCurSel();

	// empty the strings in the chapter combo box
	for(int ni = 0; ni < oldChapCount ; ni++)
	m_cmbChapter.DeleteString(0);


    // get the total number of chapters in the newly selected book
	int totalChap = _tstoi(CSwordBibleDoc::bookCode[selectedBook][2]);

   // add the "chapter number" strings in the chapter combo box
	CString indexString;
	for(int nni = 0; nni < totalChap ; nni++)  
	{ 
		indexString.Format(_T("%d"),nni + 1);
		m_cmbChapter.AddString(indexString);
	}

	m_cmbChapter.SetCurSel(0);  // set the chapter to 1

	// empty the strings in the verse combo box
	for(int ni = 0; ni < oldVerseCount ; ni++)
	m_cmbVerse.DeleteString(0);


    // get the total number of verses in the newly selected chapter
	int totalVerses = CSwordBibleDoc::VerseCount[selectedBook][0];


   // add the "Verse number" strings in the Verse combo box
	for(int nni = 0; nni < totalVerses ; nni++)
	{ 
		indexString.Format(_T("%d"),nni + 1);
		m_cmbVerse.AddString(indexString);
	}

		
  // set the verse selection to 1
	m_cmbVerse.SetCurSel(0);

	
   oldChapCount = totalChap;
    oldVerseCount = totalVerses;

}



void CVersePicker::OnSelectChapter()
{

	// the newly selected chapter from the notes combo box
	int selectedChapter  = m_cmbChapter.GetCurSel();

	// empty the strings in the verse combo box
	for(int ni = 0; ni < oldVerseCount ; ni++)
	m_cmbVerse.DeleteString(0);

    // get the total number of verses in the newly selected chapter
	int totalVerses = CSwordBibleDoc::VerseCount[m_cmbBook.GetCurSel()][selectedChapter];


   // add the "Verse number" strings in the Verse combo box
	CString indexString;
	for(int nni = 0; nni < totalVerses ; nni++)
	{ 
		indexString.Format(_T("%d"),nni + 1);
		m_cmbVerse.AddString(indexString);
	}

	m_cmbVerse.SetCurSel(0);
	
   // set the oldVerseCount to the number of verses in the current chapter of the bible in the 
	// notes section. This variable is used later during the deletion of the strings.
	oldVerseCount = totalVerses;

}



void CVersePicker::OnBnClickedOk()
{
	key.Format(_T("%s %d:%d"),CSwordBibleDoc::bookCode[m_cmbBook.GetCurSel()][0],
		m_cmbChapter.GetCurSel() + 1, m_cmbVerse.GetCurSel() + 1);

	OnOK();
}
